package com.weijx.dishes.service.impl;
import com.weijx.dishes.entity.Product;
import com.weijx.dishes.utils.IdGenerator;
import com.weijx.dishes.entity.Order;
import com.weijx.dishes.entity.OrderDetail;
import com.weijx.dishes.mapper.OrderMapper;
import com.weijx.dishes.mapper.OrderDetailMapper;
import com.weijx.dishes.mapper.ProductMapper;
import com.weijx.dishes.service.OrderService;
import com.weijx.dishes.service.EmailService;
import com.weijx.dishes.service.WeChatService;
import lombok.var;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
/**
 * com.weijx.dishes.service.impl.OrderServiceImpl
 * 描述：
 * on 2025/10/24-下午3:44
 *
 * @author weijx
 */


@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private EmailService emailService;

    @Resource
    private WeChatService weChatService;

    @Value("${server.port:78}")
    private String serverPort;

    @Override
    @Transactional
    public String createOrder(Order order) {
        // 生成订单ID
        String orderId = IdGenerator.generateId();
        order.setId(orderId);
        order.setStatus(0);
        order.setOrderStatus(0);

        // 保存订单
        orderMapper.insertOrder(order);

        // 处理订单详情
        if (order.getOrderDetails() != null && !order.getOrderDetails().isEmpty()) {
            // 获取餐品名称
            List<String> productIds = order.getOrderDetails().stream()
                    .map(OrderDetail::getProductId)
                    .collect(Collectors.toList());

            var products = productMapper.selectByIds(productIds);
            var productMap = products.stream()
                    .collect(Collectors.toMap(Product::getId, Product::getName));

            // 设置订单详情
            for (OrderDetail detail : order.getOrderDetails()) {
                detail.setId(IdGenerator.generateId());
                detail.setOrderId(orderId);
                detail.setProductName(productMap.get(detail.getProductId()));
            }

            // 批量保存订单详情
            orderDetailMapper.batchInsertOrderDetails(order.getOrderDetails());
        }

        emailService.sendOrderNotificationToMe(order);
        weChatService.sendOrderNotificationToHandler(order);

        return orderId;
    }

    @Override
    public Order getOrderById(String id) {
        return orderMapper.selectOrderById(id);
    }

    @Override
    @Transactional
    public boolean processOrder(String orderId, Integer orderStatus, String processNote) {
        // 更新订单状态
        int result = orderMapper.updateOrderStatus(orderId, orderStatus, processNote);

        if (result > 0) {
            // 获取订单信息
            Order order = orderMapper.selectOrderById(orderId);
            if (order != null) {
                // 发送处理结果邮件给女朋友
                emailService.sendProcessResultToGirlfriend(order);
                weChatService.sendProcessResultToCustomer(order);
            }
            return true;
        }
        return false;
    }
}
